# config valid only for current version of Capistrano
lock '3.4.0'
set :application, 'storage_server'
set :repo_url, 'git@git.oschina.net:traynor/storage_server.git'

# Default branch is :master
set :branch, 'master'

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/home/rntd/www/storage/#{:application}"

# Default value for :scm is :git
set :scm, :git

# the user login on the remote server
# used to connect and deploy
set :deploy_user, "rntd"

# set server pwd
set :scm_passphrase, "rntd12114"

# user sudo
set :use_sudo, false

#rails env
set :rails_env, "production"

#deploy via
set :deploy_via, :copy

# Default value for :format is :pretty
set :format, :pretty

# Default value for :log_level is :debug
set :log_level, :debug

# Default value for :pty is false
set :pty, true

# set :sync_directories, ["public/system/images"]
# set :sync_backups, 3
# set :db_file, "mongoid.yml"
# set :db_drop, '--drop' # drop database (rewrites everything)

# Default value for :linked_files is [] 'config/database.yml',
# set :linked_files, fetch(:linked_files, []).push( 'config/database.yml','config/secrets.yml') 

# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 5

#set puma 
set :puma_rackup, -> { File.join(current_path, 'config.ru') }
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"
set :puma_conf, "#{shared_path}/puma.rb"
set :puma_access_log, "#{shared_path}/log/puma_error.log"
set :puma_error_log, "#{shared_path}/log/puma_access.log"
set :puma_role, :app
set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))
set :puma_threads, [0, 16]
set :puma_workers, 0
set :puma_init_active_record, true
set :puma_preload_app, true

namespace :deploy do
  desc "db:migration fakes"
  task :migrate => :environment do
    p 'No. We will not migrate!'
  end
  
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
    # Your restart mechanism here, for example:
    # execute :touch, release_path.join('tmp/restart.txt')
    #
    # The capistrano-unicorn-nginx gem handles all this
    # for this example
    end
  end
  
  task :mongoid_create_indexes do
    on roles(:web) do
      run "cd #{deploy_to}/current/; RAILS_ENV=production bundle exec rake db:mongoid:create_indexes"
    end
  end
  
  task :compile_assets do
    on roles(:web) do
      # run "cd #{deploy_to}/current/; RAILS_ENV=production bundle exec rake assets:precompile "
    end
  end
  
  task :mongoid_migrate_database do
    on roles(:web) do
      run "cd #{deploy_to}/current/; RAILS_ENV=production bundle exec rake db:migrate"
    end
  end
  
  task :restart_ngnx do
    on roles(:web) do
      run "#{sudo} service nginx #{command}"
    end
  end
  
  task :restart_ngnx do
    on roles(:web) do
      run "#{sudo} service nginx #{command}"
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
    # Here we can do anything such as:
    # within release_path do
    #   execute :rake, 'cache:clear'
    # end
      # run "cd #{deploy_to}/current/; RAILS_ENV=production bundle exec rake db:migrate"
    end
  end
end

# after "deploy:finalize_update","deploy:symlink", :init_shared_path, :link_shared_files# , :mongoid_migrate_database , :compile_assets
# before "deploy:update_code", "sidekiq:quiet"
# after "deploy:update", "sidekiq:stop"
